package com.kgcx.coupon.rabbitmq.consumer;

import com.alibaba.fastjson.JSONObject;
import com.kgcx.common.client.order.pojo.Order;
import com.kgcx.coupon.domain.CouponInfo;
import com.kgcx.coupon.mapper.CouponInfoMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * Created by zhulong on 17/11/13.
 * 订单成功修改优惠卷状态
 */
@Component

public class AutoDispatchSuccessListener {

    private static final Logger LOG = LoggerFactory.getLogger(AutoDispatchSuccessListener.class);

    @Autowired
    private CouponInfoMapper couponInfoMapper;

    @RabbitListener(queues = "kgcxCouponStatusSuccessQueue", errorHandler = "custRabbitListenerErrorHandler")
    public void processCouponStatus(String json) {
        try {
            Order order = JSONObject.parseObject(json, Order.class);
            CouponInfo couponInfo = new CouponInfo();
            couponInfo.setKid(Long.valueOf(order.getCouponId().toString()));
            couponInfo.setState(CouponInfo.STATE_USED);
            couponInfoMapper.updateByPrimaryKeySelective(couponInfo);
        }catch (Exception e){
            LOG.error("json:"+json+"---invoke error",e);
        }
    }
}
